package com.sphd.restaurantsystem.activitys.funding;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.restaurantsystem.R;
import com.sphd.restaurantsystem.adapters.InvestorAdapter;
import com.sphd.restaurantsystem.base.BaseActivity;
import com.sphd.restaurantsystem.base.RestaurantUrl;
import com.sphd.restaurantsystem.info.InvestRuleInfo;
import com.sphd.restaurantsystem.info.InvestorInfo;
import com.sphd.restaurantsystem.info.RestaurantInfo;
import com.sphd.restaurantsystem.presenter.FundingPresenterImpl;
import com.sphd.restaurantsystem.utils.Constant;
import com.sphd.restaurantsystem.view.FundingView;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 投资人管理界面
 */
public class FundingMangerActivity extends BaseActivity implements FundingView, View.OnClickListener {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    @BindView(R.id.add_Shareholder)
    TextView addShareholder;
    private View heardView;
    private TextView sum, mAccountMoney, mAccountPermillage, deal, mImportNo, mRepealNo;
    private LinearLayout goToset, inPut, outPut;
    private FundingPresenterImpl fundingPresenter;
    private InvestorAdapter investorAdapter;
    private List<InvestorInfo> investorInfos;
    private Bundle bundle;
    private Intent intent;
    private RestaurantInfo restaurantInfo;
    List<InvestRuleInfo> investRuleInfos;
    private boolean isAllClose = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_funding_manger);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    /**
     * 时间监听 包含 RecycleView
     */
    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());
        deal.setOnClickListener(this);
        goToset.setOnClickListener(this);
        inPut.setOnClickListener(this);
        outPut.setOnClickListener(this);
        addShareholder.setOnClickListener(this);
        myRecy.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                bundle.putSerializable(Constant.INVESTRULE, investRuleInfos.get(0));
                bundle.putSerializable(Constant.INVESTOR, (Serializable) baseQuickAdapter.getItem(i));
                intent.putExtras(bundle);
                intent.setClass(context, AccountDetailsActivity.class);
                startActivityForResult(intent,1);
            }
        });

        mySwf.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                addData();
            }
        });
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        fundingPresenter = new FundingPresenterImpl(this);
        setTitleBar(toolbar, "投资管理");
        setRefresh(mySwf);
        setRecy(myRecy);
        initHeard();
        initAdapter();
        addData();
    }

    private void addData() {
        fundingPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.FUNDING + "cgid=" + restaurantInfo.getId());
    }

    private void initAdapter() {
        investorAdapter = new InvestorAdapter(R.layout.item_investor, investorInfos, context);
        myRecy.setAdapter(investorAdapter);
        investorAdapter.addHeaderView(heardView);
    }

    private void initHeard() {
        heardView = View.inflate(context, R.layout.funding_heard, null);
        sum = (TextView) heardView.findViewById(R.id.m_sum);
        mAccountMoney = (TextView) heardView.findViewById(R.id.m_account_money);
        mAccountPermillage = (TextView) heardView.findViewById(R.id.m_account_permillage);
        goToset = (LinearLayout) heardView.findViewById(R.id.go_set_layout);
        mImportNo = (TextView) heardView.findViewById(R.id.m_import_no);
        mRepealNo = (TextView) heardView.findViewById(R.id.m_repeal_no);
        inPut = (LinearLayout) heardView.findViewById(R.id.input_money_layout);
        outPut = (LinearLayout) heardView.findViewById(R.id.output_money_layout);
        deal = (TextView) heardView.findViewById(R.id.deal);
        deal.setText(Html.fromHtml("<u>" + "《投资合作协议》" + "</u>"));
    }

    @Override
    public void setSumText(double sumNo) {
        sum.setText(turn(sumNo,Constant.DECIMAL_POINT_TWO));
    }

    /**
     * 当状态为0的时候跳转
     */
    @Override
    public void startSetFunding() {
        intent.putExtra(Constant.TYPE, 0);
        intent.setClass(context, SetFundingActivity.class);
        startActivityForResult(intent, 1);
    }

    /**
     * @param repeal_apply 撤资申请的数量
     */
    @Override
    public void setrepealNo(int repeal_apply) {
        mRepealNo.setText(repeal_apply == 0 ? "暂无" : String.valueOf(repeal_apply));
    }

    /**
     * @param import_apply 投入申请的数量
     */
    @Override
    public void setimportNo(int import_apply) {
        mImportNo.setText(import_apply == 0 ? "暂无" : String.valueOf(import_apply));
    }

    /**
     * @param investRuleInfos 设置分红比例等····
     */
    @Override
    public void setResRule(List<InvestRuleInfo> investRuleInfos) {
        mAccountMoney.setText("最低投资金额:" + turn(investRuleInfos.get(0).getAccount_money()));
        mAccountPermillage.setText("分红千分比:" + turn(investRuleInfos.get(0).getAccount_permillage()) + "‰");
        this.investRuleInfos = investRuleInfos;
    }

    /**
     * @param investorInfos 获取对应的数据刷新页面
     */
    @Override
    public void setRfAdapter(List<InvestorInfo> investorInfos) {
        investorAdapter.setNewData(investorInfos);

    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.deal:
                //明细
                bundle.putSerializable(Constant.INVESTRULE, investRuleInfos.get(0));
                intent.putExtras(bundle);
                intent.putExtra(Constant.TAG, FundingMangerActivity.class.getSimpleName());
                intent.setClass(context, DealActivity.class);
                startActivity(intent);
                break;
            case R.id.go_set_layout:
                //1为开启，0为关闭状态
                for (InvestorInfo investorInfo : investorAdapter.getData()) {
                    if (investorInfo.getAccount_status() == 1) {
                        isAllClose = false;
                        break;
                    }
                }
                bundle.putInt(Constant.TYPE, 1);
                bundle.putSerializable(Constant.INVESTRULE, investRuleInfos.get(0));
                bundle.putBoolean(Constant.CLOSEALL, isAllClose);
                intent.putExtras(bundle);
                intent.setClass(context, SetFundingActivity.class);
                startActivityForResult(intent, 1);
                break;
            case R.id.input_money_layout:
                //投入申请
                bundle.putInt(Constant.TYPE,1);
                bundle.putSerializable(Constant.INVESTRULE, investRuleInfos.get(0));
                intent.putExtras(bundle);
                intent.setClass(this, ApplyForActivity.class);
                startActivityForResult(intent, 1);
                break;
            case R.id.output_money_layout:
                //撤资申请
                bundle.putInt(Constant.TYPE,0);
                bundle.putSerializable(Constant.INVESTRULE, investRuleInfos.get(0));
                intent.putExtras(bundle);
                intent.setClass(this, ApplyForActivity.class);
                startActivityForResult(intent, 1);
                break;
            case R.id.add_Shareholder:
                //新增投资人
                bundle.putSerializable(Constant.INVESTRULE, (Serializable) investRuleInfos);
                intent.putExtras(bundle);
                intent.setClass(context,AddShareholderActivity.class);
                startActivityForResult(intent,1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==1){
            addData();
        }
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
        mySwf.setRefreshing(false);
    }
}
